import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-10-05
 * Time: 21:45
 */
public class Demo {
    public static void main(String[] args) {
        System.out.println("请输入要扫描的文件路径");
        Scanner scanner = new Scanner(System.in);
        String path = scanner.next();
        File rootPath = new File(path);
        //判断用户输入的扫描文件是否合法
        if (!rootPath.isDirectory()) {
            System.out.println("您输入的扫描文件的路径有误");
            return;
        }
        System.out.println("请输入要删除的文件的关键词");
        String word = scanner.next();

        scanDir(rootPath,word);
    }

    private static void scanDir(File rootPath, String word) {
        File[] files = rootPath.listFiles();
        if(files == null) return;
        for (File f : files) {
            //添加一个日志，知道遍历到哪个文件了
            System.out.println("当前扫描文件" + f.getAbsolutePath());
            if (f.isFile()) {
                chechDelete(f,word);
            }else {
                scanDir(f,word);
            }
        }
    }

    private static void chechDelete(File f, String word) {
        if (!f.getName().contains(word)) return;
        System.out.println("当前文件为" + f.getAbsolutePath() + ", 请确认是否要删除(Y/N)");
        Scanner scanner = new Scanner(System.in);
        String choice = scanner.next();
        if (choice.equals("Y") || choice.equals("y")) {
            f.delete();
            System.out.println("删除完毕");
        }else {
            System.out.println("取消删除");
        }
    }
}
